## Refer https://www.elastic.co/guide/en/logstash/current/installing-logstash.html#package-repositories
## Refer http://blog.yunfei.me/blog/ELK-stack.html

class smx_logstash {
  require smx_java

  yumrepo { "logstash-2.4":
    baseurl  => "https://packages.elastic.co/logstash/2.4/centos",
    descr    => "Logstash repository for 2.4.x packages",
    enabled  => 1,
    gpgcheck => 0,
  }

  package { "logstash" :
    ensure  => installed,
    before  => Notify['pkg-logstash-install'],
    require => Yumrepo['logstash-2.4'],
  }

  service { 'logstash' :
    enable  => true,
    ensure  => running,
    require => Package['logstash'],
  }


  file { "/etc/logstash/conf.d/logstash-nginx.conf":
    ensure  => file,
    group   => root, owner  => root,
    mode    => 644,
    require => Package['logstash'],
    source  =>  "puppet:///modules/smx_logstash/logstash-nginx.conf",
  }

  file { "/opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-2.0.5/patterns/grok-patterns" :
    ensure  => file,
    group   => logstash, owner  => logstash,
    mode    => 644,
    require => Package["logstash"],
    source  =>  "puppet:///modules/smx_logstash/grok-patterns",
  }

  exec { "seduser" :
    command => "sed -i -e 's/LS_USER=logstash/LS_USER=root/' /etc/init.d/logstash",
    path    => [ "/bin","/sbin","/usr/bin","/usr/sbin" ],
    require => Package["logstash"],
  }

  exec { "sedgroup" :
    command => "sed -i -e 's/LS_GROUP=logstash/LS_GROUP=root/' /etc/init.d/logstash",
    path    => [ "/bin","/sbin","/usr/bin","/usr/sbin" ],
    require => Exec["seduser"],
    notify  => Service['logstash'],
  }

  notify { 'pkg-logstash-install':
    message => '----- logstash-2.4 pkgs installation completed -----',
  }
}